package module.app
{
	import drongo.desktop.language.AppLanguage;
	import drongo.desktop.utils.FileUtil;
	import drongo.desktop.window.WindowManager;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	
	import common.WindowKeys;
	import common.view.FGUIMediator;
	
	import fairygui.UIConfig;
	import fairygui.UIPackage;
	
	import module.map.MapMediator;
	import module.map.MapNoticeWindow;
	import module.project.ProjectMediator;
	import module.project.ProjectNativeWindow;
	
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
	import robotlegs.bender.framework.api.IContext;
	import robotlegs.bender.framework.api.IInjector;
	
	import ui.Builder.BuilderBinder;
	import ui.Builder.UI_OpenProjectWindow;
	import ui.Map.MapBinder;
	import ui.Map.UI_MapWindow;
	
	public class AppMediator extends FGUIMediator
	{
		[Inject]
		public var mediatorMap:IMediatorMap;
		[Inject]
		public var inject:IInjector;
		
		public function AppMediator()
		{
			super();
		}
		override public function initialize():void{
			super.initialize();
			//初始化语言包
			AppLanguage.init(AppLanguage.LANGUAGE_CONFIG,"res/locales/");
			//加载UI资源
			this.LoadUIAsset();
		}
		//==================================加载UI资源
		private var uiAssetFiles:Array;
		private function LoadUIAsset():void{
			this.uiAssetFiles=FileUtil.getDirectoryListing(new File(File.applicationDirectory.nativePath+File.separator+"res"+File.separator+"ui"+File.separator),new FileFilter("uiZip","*.zip"));
			this.TryNext();
		}
		
		private function TryNext():void{
			if(this.uiAssetFiles.length>0){
				var file:File=this.uiAssetFiles.pop();
				file.addEventListener(Event.COMPLETE,this.UIAssetLoaded);
				file.load();
			}else{
				UIPackage.waitToLoadCompleted(this.AllUIAssetLoaded);
			}
		}
		
		private function UIAssetLoaded(e:Event):void{
			var file:File=e.target as File;
			file.removeEventListener(Event.COMPLETE,this.UIAssetLoaded);
			UIPackage.addPackage(file.data,null).loadAllImages();
			this.TryNext();
		}
		
		private function AllUIAssetLoaded():void{
			UIConfig.defaultScrollBounceEffect = false;
			UIConfig.defaultScrollTouchEffect = false;
			UIConfig.modalLayerAlpha = 0;
			UIConfig.verticalScrollBar = "ui://Basic/ScrollBar_VT";
			UIConfig.horizontalScrollBar = "ui://Basic/ScrollBar_HZ";
			UIConfig.popupMenu = "ui://Basic/PopupMenu";
			UIConfig.popupMenu_seperator = "ui://Basic/MenuSeperator";
			UIConfig.tooltipsWin = "ui://Basic/TooltipsWin";
			UIConfig.defaultComboBoxVisibleItemCount = int.MAX_VALUE;
			/*			UIObjectFactory.setPackageItemExtension("ui://Basic/TextInput",TextInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/TextArea",TextInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/NumericInput",NumericInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/ColorInput",ColorInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/ColorInputWithPreset",ColorInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/ResourceInput",ResourceInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/ListItemInput",ListItemInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/ListItemResourceInput",ListItemResourceInput);
			UIObjectFactory.setPackageItemExtension("ui://Basic/EditableTreeItem",EditableTreeItem);
			UIObjectFactory.setPackageItemExtension("ui://Basic/LinkButton",LinkButton);
			UIObjectFactory.setPackageItemExtension("ui://Builder/ControllerPageInput",ControllerPageInput);
			UIObjectFactory.setPackageItemExtension("ui://Builder/Timeline",TimelineComponent);
			UIObjectFactory.setPackageItemExtension("ui://Builder/GearPropsItem",GearPropsItem);
			UIObjectFactory.setPackageItemExtension("ui://Builder/ChildObjectInput",ChildObjectInput);*/
			
			BuilderBinder.bindAll();
			
			MapBinder.bindAll();
			
			
			this.InitWindows();
		}
		
		/**
		 * 初始化窗口
		 */
		private function InitWindows():void{
			//映射
			mediatorMap.map(UI_OpenProjectWindow).toMediator(ProjectMediator);
			mediatorMap.map(UI_MapWindow).toMediator(MapMediator);
			
			WindowManager.register(WindowKeys.PROJECT,ProjectNativeWindow);
			WindowManager.register(WindowKeys.MAP,MapNoticeWindow);
			
			//关闭splash
			var splashWindow:SplashWindow=inject.getInstance(SplashWindow);
			splashWindow.close();
			
			var content:IContext=inject.getInstance(IContext);
			content.destroy();
			WindowManager.call(WindowKeys.PROJECT);
		}
	}
}